/*
 * @name: 推荐类目管理
 * @Author: along
 * @Edition: 1.3.5
 * @Date: 2020-04-23
 */
<template>
    <div class="container goodsAgentCategoryRecommend">
        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="goodsAgentCategoryRecommend"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="180"
                label="序号"
            />
            <el-table-column
                prop="cate_name"
                label="代理类目"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="recommend_sort"
                label="排序"
                :formatter="emptyFormatter"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.recommend_sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            style="color: #606266"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="cursor-font"
                        v-if="scope.row.recommend_status == 0"
                        @click="fnSetRecommend(scope.row)"
                    >
                        设为推荐
                    </el-button>
                    <el-button
                        type="text"
                        class="cursor-tip"
                        v-if="scope.row.recommend_status == 1"
                        @click="fnRelRecommend(scope.row)"
                    >
                        取消推荐
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <!-- <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div> -->
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    /**
     * @param   {Object}     filterData             排序字段
     * @param   {Object}     page                   分页
     * @param   {Array}      tableData              数据列表
     * @param   {OBject}     authResult             权限列表
     */
    mixins: [back],
    data () {
        return {
            filterData: {
                sort_field: 'asc',
                sorttype: 'desc'
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '商城'},{title: '推荐类目管理'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/shop_goods_agent_cate%5Cget_list', {
                recommend_status: 1,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    // this.page.now_page = resp.data.page.now_page;
                    // this.page.total_count = resp.data.page.total_count;
                    // this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 设为推荐
         */
        fnSetRecommend (row) {
            this.$post('/student/shop_goods_agent_cate%5Cmodify',{
                cate_no: row.cate_no,
                recommend_status: 1
            }, resp => {
                if(resp.code == 1) {
                    this.getTableData();
                    this.$message({
                        type: 'success',
                        message: '设为推荐成功',
                        center: true
                    });
                } else {
                    this.$message({
                        type: 'waring',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 取消推荐
         */
        fnRelRecommend (row) {
            this.$confirm('是否确认？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/shop_goods_agent_cate%5Cmodify',{
                    cate_no: row.cate_no,
                    recommend_status: 0
                }, resp => {
                    if(resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '取消成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 修改排序
         */
        fnEditSort(row) {
            this.$prompt('修改排序', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^(0|\+?[1-9][0-9]*)$/,
                inputPlaceholder: '请输入序号',
                inputErrorMessage: '请输入正整数',
                closeOnClickModal: false,
            }).then(({ value }) => {
                this.$post('/student/shop_goods_agent_cate%5Cmodify', {
                    cate_no: row.cate_no,
                    recommend_sort: value
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '修改成功',
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 数据排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 'asc',
                descending: 'desc'
            };

            let sortFieldMap = {
                recommend_sort: 'recommend_sort'
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .cursor-tip {
            color: rgb(228, 18, 11);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .dialog-footer {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .zd-tips {
        padding: 0 30px;
        line-height: 22px;
        text-align: center;
        margin-bottom: 30px;
        color: #FF0000;
    }
</style>
<style lang="less">
    .goodsAgentCategoryRecommend{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .upload-good-image{
            border: 1px solid red;
            width: 200px;
            height: 200px;
            display: flex;
            border: 1px dashed #d9d9d9;
            border-radius: 4px;
            align-items: center;
            justify-content: center;
            font-size: 34px;
            color: #8c939d;
            img{
                width: 200px;
                height: 200px;
            }
            .upload-hide {
                width: 200px;
                height: 200px;
                display: flex;
                justify-content: center;
                align-items: center;
            }
        }
    }
</style>